<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$aCamForm = array();
require("_inc/const.campiAsez.php");

if (reqVar('azione') == 'salva') {
	$msgPag = conferma($aCamForm);
} 

$sqlSel = "SELECT s.* FROM ovf_anag_sez s WHERE s.cod_sez = '".reqVarAS('cod')."'";
$objDB->exec($sqlSel);
if(!$aRow = $objDB->fetch()) {
	backOnErr("id. errato");
}
reqDef('descr',$aRow['descr']);
reqDef('seq',$aRow['seq']);
reqDef('cod_sez',$aRow['cod_sez']);
reqDef('tipo_sez',$aRow['tipo_sez']);
reqDef('url_ext',$aRow['url_ext']);

$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:document.frm1.submit();\" >salva</a>";
$sCmdR .= cmdTorna('torna');

testaHtml('Gestione sezioni anagrafica','Modifica sezione',$msgPag,'gest');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('cod');?>
<?=getHtmlHidden('tipo_sez');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Sezione anagrafica</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm);
if (reqvar('tipo_sez')=='ext') {
?>
   <tr>
      <td class="lab1" valign='top' colspan='4'>URL</td>
      <td class="txt1" colspan='16' align='left'>
			<?=getHtmlText('url_ext',100,255)?>
		</td>
   </tr>	
<?
} 
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function conferma($aCamForm) {
	global $objDB;
	$msg = '';
	if (preg_match("/[^-_a-z0-9]/",reqVar('cod_sez'))) {
		$msg = "Codice '".reqVar('cod_sez')."' puo' contenere solo lettere numeri '-' e '_'<br>";
	}
	if (preg_match("/[^0-9]/",reqVar('seq')) || reqVarTrim('seq')=='') {
		$msg = "campo 'Sequenza' non numerico o vuoto<br>";
	}
	if (reqVarTrim('descr')=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	if (reqVar('tipo_sez')=='ext' && reqVarTrim('url_ext')=='') {
		$msg .= "URL obbligatoria<br>";
	}
	/*////////////////////////////////////
	$_REQUEST['cod_sez'] = strtolower(trim(reqVar('cod_sez')));
	if (reqVar('cod_sez')=='') {
		$msg .= "Codice obbligatorio<br>";
	}
	if (reqVar('cod_sez') != reqVar('cod')) {
		$sqlSel = "SELECT cod_sez FROM ovf_anag_sez " .
				" WHERE cod_sez = '".reqVarAS('cod_sez')."'";
		$objDB->exec($sqlSel,'insIst');
		$aRow = $objDB->fetch('insIst');
		if ($aRow) {
			$msg .= "Codice gia' esistente<br>";
		}
	}
	//*////////////////////////////////////
	if ($msg != '') {
		return $msg;
	}
	$sSql = "UPDATE ovf_anag_sez SET descr = '".reqVarAS('descr')."', seq = ".reqVarInt('seq');
	if (reqVar('tipo_sez')=='ext') {
		$sSql .= ", url_ext = '".reqVarAS('url_ext')."'";
	}
	$sSql .= " WHERE cod_sez = '".reqVarAS('cod')."'";
	$objDB->exec($sSql,'insRec');
	
	header("Location: ".urlTorna(_t("sezione modificata")));
	exit();
}
